package com.alex.server.exception;

import com.alex.server.pojo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 * @RestControllerAdvice都是对Controller进行增强的，可以全局捕获spring mvc抛的异常。
 * @ExceptionHandler (value = Exception.class)
 */



public class GlobalException {
    @ExceptionHandler(value = SQLException.class)
    public RespBean mySqlException(SQLException e){
        if (e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据,操作失败");
        }
        return RespBean.error("数据库异常,操作失败");
    }
}
